<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="__PUBLIC__/jquery-3.0.0.js"></script>
        
    </head>
    <body>
        手机号：<input name="tel" id="tel" type="text" /><span id="teltest">注意：需要一个合法的手机号，当需要获取新密码或获取特定消息及通知时会使用此号码。</span>
        <br/>
        验证码：<input name="vdcodetel" id="vdcodetel" type="text"/><input id ="getpwd" type="button" onclick="getpwd()" value="获取验证码"/>
        <script>
        var setIntervals;
        var time
        var count = 60;
        time = count;
        function getpwd(){        
            var tel = $('#tel').val();
            if(!tel ==""){
            //验证手机有效性
               var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
               if(!myreg.test($('#tel').val())){ 
                    $("#teltest").html("*请输入正确的手机号码！").css("color","red"); 
                }else{
                    var url = "<?php echo U('Home/Home/sms')?>";
                var data= {};
                data ['tel'] = $('#tel').val();
                var dataType = "json";
                $.post(
                    url,
                    data,
                    function(data){                      
                        if(data.status === 0){
                            $("#getpwd").attr('disabled','true');
                            setIntervals = setInterval(function (){SetRemainTime();}, 1000);
                        }
                        if(data.status === 1){
                            $("#p").html("*请检查电话号码是否正确。").css("color","red");
                        }
                        if(data.status === 2){
                            $("#p").html("*发送失败请从新发送！").css("color","red");
                        }
                    },
                    dataType
                );
                return false;
                }
            }else{
                $("#teltest").html("*请输入您的手机号码。").css("color","red");
            }
            function SetRemainTime() {
                if (time === 0) {                
                    window.clearInterval(setIntervals);//停止计时器
                    $("#teltest").html('验证码到期请重新发送').css("color","red");
                    $("#getpwd").removeAttr("disabled");//启用按钮
                    $("#getpwd").val("重新发送验证码");
                    session('pwd',null); //清除验证码。如果不清除，过时间后，输入收到的验证码依然有效
                }else {
                    time--;
                    $("#teltest").html('验证码发送成功，请在' + time + '秒内输入验证码').css("color","green");
                }
            }
        }   
        </script>
    </body>
</html>
